package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //  添加
    @PostMapping
    public R add(@RequestBody Category category) {
        categoryService.save(category);
        return R.success("添加成功");
    }

    //  分页
    @GetMapping("/page")
    public R findPage(Integer page, Integer pageSize) {
        R r = categoryService.findPage(page, pageSize);
        return r;
    }

    /**
     * 删除分类 注意: 菜品分类和套餐分类内有菜品和套餐时 是不能直接删除的
     */
    @DeleteMapping
    public R deleteById(Long id) {
        R r = categoryService.deleteById(id);
        return r;
    }

    @PutMapping
    public R update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R list(Integer type) {
        R r = categoryService.findList(type);
        return r;
    }

}
